<?php

class Result extends BaseResult
{   
    public function getTotal()
    {
        $total_count = 0;
        foreach ($this->getResultAnswerss() as $result_answer)
        {
            if($result_answer->getResult())
            {
                $total_count += $result_answer->getQuestion()->getMark();
            }
        }
        return $total_count;
    }
    
    public function getTimeToAnd()
    {        
        return strtotime($this->data) + $this->getTest()->getTime() * 60 - time();
    }
    
    public function hasTime()
    {
        if(strtotime($this->data) + $this->getTest()->getTime() * 60 > time())
        return true;
        else
        return false;
    }

    public function getResultAnswerByQuestion($id)
    {
        $c = new Criteria();
        $c->add(ResultAnswersPeer::RESULT_ID, $this->id);
        $c->add(ResultAnswersPeer::RESULT, null);
        $c->add(ResultAnswersPeer::QUESTION_ID, $id);
        return ResultAnswersPeer::doSelectOne($c);
    }
    
    public function getResultAnswer($id)
    {
        $c = new Criteria();
        $c->add(ResultAnswersPeer::RESULT_ID, $this->id);
        $c->add(ResultAnswersPeer::RESULT, null);
        $c->add(ResultAnswersPeer::ID, $id);
        return ResultAnswersPeer::doSelectOne($c);
    }
    public function getNextResultAnswer($id)
    {
        $c = new Criteria();
        $c->add(ResultAnswersPeer::RESULT_ID, $this->id);
        $c->add(ResultAnswersPeer::RESULT, null);
        $c->add(ResultAnswersPeer::ID, $id, Criteria::GREATER_THAN);
        $c->addAscendingOrderByColumn(ResultAnswersPeer::ID);
        return ResultAnswersPeer::doSelectOne($c);
    }
    public function getPrevResultAnswer($id)
    {
        $c = new Criteria();
        $c->add(ResultAnswersPeer::RESULT_ID, $this->id);
        $c->add(ResultAnswersPeer::RESULT, null);
        $c->add(ResultAnswersPeer::ID, $id, Criteria::LESS_THAN);
        $c->addDescendingOrderByColumn(ResultAnswersPeer::ID);
        return ResultAnswersPeer::doSelectOne($c);
    }
    public function getFirstResultAnswer()
    {
        $c = new Criteria();
        $c->add(ResultAnswersPeer::RESULT_ID, $this->id);
        $c->add(ResultAnswersPeer::RESULT, null);
        $c->addAscendingOrderByColumn(ResultAnswersPeer::ID);
        return ResultAnswersPeer::doSelectOne($c);
    }
    public function getLastResultAnswer()
    {
        $c = new Criteria();
        $c->add(ResultAnswersPeer::RESULT_ID, $this->id);
        $c->add(ResultAnswersPeer::RESULT, null);
        $c->addDescendingOrderByColumn(ResultAnswersPeer::ID);
        return ResultAnswersPeer::doSelectOne($c);
    }
    public function getNotAnsweredResultAnswers()
    {
        $c = new Criteria();
        $c->add(ResultAnswersPeer::RESULT_ID, $this->id);
        $c->add(ResultAnswersPeer::RESULT, null);
        return ResultAnswersPeer::doSelect($c);
    }
}
